package com.maidou.qiantai.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.maidou.qiantai.vo.SpuCategoryTreeVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.maidou.qiantai.domain.SpuCategory;
import com.maidou.qiantai.service.ISpuCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品三级分类Controller
 * 
 * @author maidou
 * @date 2024-03-20
 */
@RestController
@RequestMapping("/qiantai/category")
public class SpuCategoryController extends BaseController
{
    @Autowired
    private ISpuCategoryService spuCategoryService;


    /**
     * 查询商品三级分类列表
     */
//    @PreAuthorize("@ss.hasPermi('qiantai:category:list')")
    @GetMapping("/list-tree")
    public AjaxResult list()
    {
//        startPage();
        List<SpuCategoryTreeVo> list = spuCategoryService.selectSpuCategoryListTree();
//       return getDataTable(list);
        return AjaxResult.success(list);
    }

    /**
     * 查询商品三级分类列表
     */
    @PreAuthorize("@ss.hasPermi('qiantai:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(SpuCategory spuCategory)
    {
        startPage();
        List<SpuCategory> list = spuCategoryService.selectSpuCategoryList(spuCategory);
        return getDataTable(list);
    }

    /**
     * 导出商品三级分类列表
     */
    @PreAuthorize("@ss.hasPermi('qiantai:category:export')")
    @Log(title = "商品三级分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SpuCategory spuCategory)
    {
        List<SpuCategory> list = spuCategoryService.selectSpuCategoryList(spuCategory);
        ExcelUtil<SpuCategory> util = new ExcelUtil<SpuCategory>(SpuCategory.class);
        util.exportExcel(response, list, "商品三级分类数据");
    }

    /**
     * 获取商品三级分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('qiantai:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(spuCategoryService.selectSpuCategoryById(id));
    }

    /**
     * 新增商品三级分类
     */
    @PreAuthorize("@ss.hasPermi('qiantai:category:add')")
    @Log(title = "商品三级分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SpuCategory spuCategory)
    {
        return toAjax(spuCategoryService.insertSpuCategory(spuCategory));
    }

    /**
     * 修改商品三级分类
     */
    @PreAuthorize("@ss.hasPermi('qiantai:category:edit')")
    @Log(title = "商品三级分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SpuCategory spuCategory)
    {
        return toAjax(spuCategoryService.updateSpuCategory(spuCategory));
    }

    /**
     * 删除商品三级分类
     */
    @PreAuthorize("@ss.hasPermi('qiantai:category:remove')")
    @Log(title = "商品三级分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        Long[] longs = new Long[]{id};
        return toAjax(spuCategoryService.deleteSpuCategoryByIds(longs));
    }
}
